# coding=utf-8

import pygame
# from scene_test import SceneTest

# 定义场景基类
class Scene:
    def __init__(self):
        self.is_gui = True
        self.next_scene = None

    def activate(self, params = None):
        pass

    def deactivate(self):
        pass

    def switch(self, scene_name, params = None):
        self.deactivate()
        return scene_name, params

    def process_events(self, events):
        pass

    def update(self, time_delta):
        pass

    def draw(self, screen):
        pass

# 定义第一个场景
class Scene1(Scene):
    def __init__(self):
        super().__init__()

    def process_events(self, events):
        print("scene1")
        for event in events:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                return self.switch("scene2")

    def draw(self, screen):
        screen.fill((255, 0, 0))
        font = pygame.font.Font(None, 74)
        text = font.render("Scene 1", True, (255, 255, 255))
        screen.blit(text, (250, 250))

# 定义第二个场景
class Scene2(Scene):
    def __init__(self):
        super().__init__()

    def process_events(self, events):
        print("scene2")
        for event in events:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                return self.switch("scene1")

    def draw(self, screen):
        screen.fill((0, 0, 255))
        font = pygame.font.Font(None, 74)
        text = font.render("Scene 2", True, (255, 255, 255))
        screen.blit(text, (250, 250))

# 场景管理器
class SceneManager:
    def __init__(self, scenes, initial_scene, initial_scene_params = None):
        self.scenes = scenes
        self.current_scene = self.scenes[initial_scene]
        self.current_scene.activate(initial_scene_params)

    def process_events(self, events):
        next_scene = self.current_scene.process_events(events)
        if next_scene:
            scene_name, params = next_scene
            scene = self.scenes[scene_name]
            scene.activate(params)
            self.current_scene = scene

        return self.current_scene.is_gui

    def update(self, time_delta):
        self.current_scene.update(time_delta)

    def draw(self, screen):
        self.current_scene.draw(screen)

# 主程序
def main():
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("Scene Switching Example")

    scenes = {
        "test": SceneTest(),
        "scene1": Scene1(),
        "scene2": Scene2(),
    }

    scene_manager = SceneManager(scenes, "test")

    running = True
    clock = pygame.time.Clock()
    while running:
        time_delta = clock.tick(60)
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                running = False

        scene_manager.process_events(events)
        scene_manager.update(time_delta)
        scene_manager.draw(screen)

        pygame.display.flip()

    pygame.quit()

if __name__ == "__main__":
    main()
